package com.joplus.housekeeper.ui.view.pickView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.TintTypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.DepartmentSpecialKeyBean;
import com.joplus.housekeeper.bean.DepartmentValueBean;
import com.joplus.housekeeper.utils.DensityUtils;
import com.joplus.housekeeper.utils.StringUtils;
import com.joplus.housekeeper.utils.TimeSelectType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author： ccm
 * time： 2017/3/14.
 * description：愉快的时间选择控件..啦啦啦
 */

public class TimePick extends PopupWindow {

    private String timeName;
    private int timeCount;
    private float timePx;
    private LoopView lv_year;
    private LoopView lv_month;
    private LoopView lv_day;
    private TextView tv_title;
    private boolean key = false;

    public TimePick(Context context) {
        super(context);
        initView(context);
    }

    public TimePick(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TimePick(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TintTypedArray tintTypedArray = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.TimePick, defStyleAttr, 0);
        timeCount = tintTypedArray.getInt(R.styleable.TimePick_timecount, 2);
        timeName = tintTypedArray.getString(R.styleable.TimePick_timeName);
        timePx = tintTypedArray.getDimension(R.styleable.TimePick_timeFirstLength, DensityUtils.dip2px(context, 40));

        tintTypedArray.recycle();
        initView(context);
    }

    public void setInitPosition(int i) {
        lv_year.setInitPosition(i);
    }

    private void initView(Context context) {
        setInputMethodMode(INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                dismiss();
                if (mActivity != null) {
                    backgroundAlpha(mActivity, 1);
                }
            }
        });
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_timepick, null);
        View tv_left = view.findViewById(R.id.tv_left);
        View tv_right = view.findViewById(R.id.tv_right);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        lv_year = (LoopView) view.findViewById(R.id.lv_year);
        lv_month = (LoopView) view.findViewById(R.id.lv_month);
        lv_day = (LoopView) view.findViewById(R.id.lv_day);

        if (!StringUtils.isEmpty(timeName)) {
            tv_title.setText(timeName);
        }
        if (timeCount == 1) {
            lv_month.setVisibility(View.GONE);
            lv_day.setVisibility(View.GONE);
        } else if (timeCount == 2) {
            lv_day.setVisibility(View.GONE);
        }
        tv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (check != null) {
                    check.time(t_year + t_month + t_day);
                }
                dismiss();
            }
        });

        lv_year.setLoopListener(new LoopScrollListener() {
            @Override
            public void onItemSelect(int item) {
                if (item > year.size() || year.size() < 1) {
                    return;
                }

                t_year = year.get(item);
                if (month.size() < 1 || month.size() < item) {
                    return;
                }
                n_month = month.get(item);

                lv_month.setDataList(n_month);


                if (yearData != null) {
                    TimeSelectType.getInstance().yearName = yearData.get(item).getDepartmentName();
                    TimeSelectType.getInstance().yearId = yearData.get(item).getDepartmentSid();
                }

            }
        });
        lv_month.setLoopListener(new LoopScrollListener() {
            @Override
            public void onItemSelect(int item) {

                if (item > n_month.size() || n_month.size() < 1) {
                    return;
                }
                Log.e("time month", n_month.size() + "," + item);
                if (timeCount == 2) {
                    t_month = n_month.get(item);
                }

                if (timeCount == 3) {
                    t_month = n_month.get(item);
                    if (key) {
                        n_day = day.get(t_year + t_month);
                    } else {
                        n_day = day.get(t_month);
                    }

                    Log.e("time day", n_day + "." + item);
                    lv_day.setDataList(n_day);
                }


                if (monthData != null && t_monthData != null) {
                    DepartmentValueBean departmentSpecialKeyBean = t_monthData.get(item);
                    TimeSelectType.getInstance().monthName = departmentSpecialKeyBean.getDepartmentName();
                    TimeSelectType.getInstance().monthId = departmentSpecialKeyBean.getDepartmentSid();
                }

            }
        });
        lv_day.setLoopListener(new LoopScrollListener() {
            @Override
            public void onItemSelect(int item) {
                if (n_day.size() <= 1) {
                    return;
                }
                if (timeCount == 3) {
                    t_day = n_day.get(item);
                }

            }
        });
        this.setContentView(view);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);


    }

    private String t_year = "";
    private String t_month = "";
    private String t_day = "";
    private List<String> n_month = new ArrayList<>();
    private List<String> n_day = new ArrayList<>();
    private List<String> year = new ArrayList<>();
    private List<List<String>> month = new ArrayList<>();
    private Map<String, List<String>> day = new HashMap<>();

    public void setYear(List<String> list) {

        year = list;
        lv_year.setDataList(list);
        t_year = list.get(0);
    }

    List<DepartmentSpecialKeyBean> yearData;

    public void setYear(List<String> list, List<DepartmentSpecialKeyBean> data) {
        year = list;
        lv_year.setDataList(list);
        t_year = list.get(0);
        yearData = data;

        TimeSelectType.getInstance().yearId = yearData.get(0).getDepartmentSid();
        TimeSelectType.getInstance().yearName = t_year;
    }

    public void setMonth(List<List<String>> list) {
        if (list.size() < 1) {
            return;
        }
        month = list;
        lv_month.setDataList(list.get(0));
        n_month = list.get(0);
        t_month = month.get(0).get(0);

    }

    List<List<DepartmentValueBean>> monthData;
    List<DepartmentValueBean> t_monthData;

    public void setMonth(List<List<String>> list, List<List<DepartmentValueBean>> data) {
        if (list.size() < 1 || list.get(0).size() < 1) {
            return;
        }
        month = list;
        lv_month.setDataList(list.get(0));
        n_month = list.get(0);
        t_month = month.get(0).get(0);
        monthData = data;
        t_monthData = data.get(0);
        TimeSelectType.getInstance().monthId = t_monthData.get(0).getDepartmentSid();
        TimeSelectType.getInstance().monthName = t_monthData.get(0).getDepartmentName();

    }

    public void setDay(Map<String, List<String>> map) {

        if (month.size() < 1) {
            return;
        }
        List<String> strings;
        day = map;
        if (key) {
            strings = day.get(t_year + month.get(0).get(0));
            n_day = day.get(t_year + t_month);
        } else {
            strings = day.get(month.get(0).get(0));
            n_day = day.get(t_month);
        }

        lv_day.setDataList(strings);

        if (key) {
            t_day = day.get(t_year + month.get(0).get(0)).get(0);
        } else {
            t_day = day.get(month.get(0).get(0)).get(0);
        }


    }

    Activity mActivity;
    private TimeCheck check;

    public void countNum(int num) {
        timeCount = num;
        if (timeCount == 1) {
            lv_month.setVisibility(View.GONE);
            lv_day.setVisibility(View.GONE);
        } else if (timeCount == 2) {
            lv_month.setVisibility(View.VISIBLE);
            lv_day.setVisibility(View.GONE);
        } else if (timeCount == 3) {
            lv_month.setVisibility(View.VISIBLE);
            lv_day.setVisibility(View.VISIBLE);
        }
    }

    public void setTitle(String text) {
        tv_title.setText(text);
    }

    public void setYearWeight() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f);
        lv_year.setLayoutParams(layoutParams);
    }

    public void setTextSize() {
        lv_year.setTextSize(20);
    }

    public void showTime(Activity a, TimeCheck check) {
        this.check = check;
        mActivity = a;
        showAtLocation(a.getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        backgroundAlpha(a, 0.5f);
    }


    //启动popupwindow背景半透明
    public static void backgroundAlpha(Activity a, float bgAlpha) {
        WindowManager.LayoutParams lp = a.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        if (bgAlpha == 1) {
            a.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            a.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        a.getWindow().setAttributes(lp);
    }

    public void setKey(boolean key) {
        this.key = key;
    }


    public interface TimeCheck {
        public void time(String time);
    }
}
